//
//  LeftTextImgBtnView.m
//  HuaXin
//
//  Created by 江伟 on 2018/11/2.
//

#import "LeftTextImgBtnView.h"
@interface LeftTextImgBtnView()
@end

@implementation LeftTextImgBtnView

/// 默认文字与图片间距
static float const lDefaultSpace = 7;

- (instancetype)initWithFrame:(CGRect)frame space:(CGFloat)space imgSize:(CGSize)size {
    if (self = [super initWithFrame:frame]) {
        _index = 0;
        _state = 0;
        _imgSize = size;
        self.backgroundColor = [UIColor whiteColor];
        if (space > 0) {
            _space = space;
        } else {
            _space = lDefaultSpace;
        }
        self.frame = frame;
        [self lSetupViews];
    }
    return self;
}

- (void)lSetupViews {
    _lab = [[UILabel alloc] initWithBackgroundColor:[UIColor whiteColor] text:@"" textColor:[UIColor colorWithHexString:@"#666666"] font:R_Font(15) textAlignment:NSTextAlignmentCenter];
    [self addSubview:_lab];
    _lab.frame = CGRectMake(0, 0, self.width - _space - _imgSize.width, self.height);
    
    _img = [[UIImageView alloc] initWithFrame:CGRectMake(_lab.right + _space, 0, _imgSize.width, _imgSize.height)];
    _img.centerY = _lab.centerY;
    _img.userInteractionEnabled = YES;
    [self addSubview:_img];
}

- (void)setTitle:(NSString *)title {
    _title = title;
    _lab.text = title;
    [_lab sizeToFit];
    _lab.centerX = self.centerX - self.left;
    _lab.centerY = self.centerY - self.top;
    _img.left = _lab.right + _space;
}

- (void)setImgSize:(CGSize)imgSize {
    _imgSize = imgSize;
    _img.left = _lab.right + _space;
    _img.size = imgSize;
}

- (void)setSpace:(CGFloat)space {
    _space = space;
    _img.left = _lab.right + _space;
}

- (void)addTapGestureTarget:(id)target action:(SEL)selector {
    if (!selector) { return; }
    if (!target) { return; }
    _tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:selector];
    [self addGestureRecognizer:_tap];
}


@end
